package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.GroupItemMapper;
import com.itheima.mapper.SetmealGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private GroupItemMapper groupItemMapper;
    @Autowired
    private SetmealGroupMapper setmealGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     */
    public void add(CheckGroup checkGroup, ArrayList<Integer> checkitemIds) {

        checkGroupMapper.insert(checkGroup);

        if(checkitemIds != null && checkitemIds.size() > 0){
            for (Integer checkitemId : checkitemIds) {
                groupItemMapper.insert(checkGroup.getId(),checkitemId);
            }
        }

    }

    /**
     * 删除检查组
     * @param id
     */
    public void deleteById(Integer id) {
        // 删除检查组和检查项的关联关系
        groupItemMapper.deleteById(id);

        // 删除检查组
        checkGroupMapper.deleteById(id);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.pageQuery(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据套餐id查询检查组id
     * @param setmealId
     * @return
     */
    public ArrayList<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        ArrayList<Integer> checkGroupIds = setmealGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        return checkGroupIds;
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     */
    public void edit(CheckGroup checkGroup, ArrayList<Integer> checkitemIds) {
        checkGroupMapper.update(checkGroup);

        // 删除检查组和检查项的关联关系
        if (checkitemIds != null && checkitemIds.size() > 0) {
            groupItemMapper.deleteByCheckGroupId(checkGroup.getId());
            for (Integer checkitemId : checkitemIds) {
                groupItemMapper.insert(checkGroup.getId(),checkitemId);
            }
        }
    }

    /**
     * 查询所有检查组
     * @return
     */
    public ArrayList<CheckGroup> findAll() {
        ArrayList<CheckGroup> checkGroups = checkGroupMapper.findAll();
        return checkGroups;
    }

    /**
     * 根据id查询检查组信息
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);

        return checkGroup;
    }
}
